package com.zf.validation.utils;

import jakarta.validation.Valid;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

/**
 * 直接使用List作为参数是无法执行List中对象里的字段的校验的，只能对List本身做校验，例如：长度、是否空等等，不能执行其内部元素的校验注解。
 * 所以使用 委托模式 解决无法直接校验 List类型的参数(@Validated List<User> paramList 校验无效) 的问题
 * 内部使用了嵌套校验。
 * (@Validated ValidationList<User> paramList 校验生效)
 */
public class ValidationList<E> implements List<E> {

    // @Valid 是使嵌套校验生效的注解，嵌套校验时可以执行List内部元素的注解校验，所以使用委托模式，重写List，在内部执行List中元素的校验
    @Valid
    public List<E> list = new ArrayList<>();

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return this.list.toString();
    }

    @Override
    public int size() {
        return this.list.size();
    }

    @Override
    public boolean isEmpty() {
        return this.list.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        return this.list.contains(o);
    }

    @Override
    public Iterator<E> iterator() {
        return this.list.iterator();
    }

    @Override
    public Object[] toArray() {
        return this.list.toArray();
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return this.list.toArray(a);
    }

    @Override
    public boolean add(E e) {
        return this.list.add(e);
    }

    @Override
    public boolean remove(Object o) {
        return this.list.remove(o);
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return this.list.containsAll(c);
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return this.list.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return this.list.addAll(index, c);
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return this.list.removeAll(c);
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return this.list.retainAll(c);
    }

    @Override
    public void replaceAll(UnaryOperator<E> operator) {
        this.list.replaceAll(operator);
    }

    @Override
    public void sort(Comparator<? super E> c) {
        this.list.sort(c);
    }

    @Override
    public void clear() {
        this.list.clear();
    }

    @Override
    public E get(int index) {
        return this.list.get(index);
    }

    @Override
    public E set(int index, E element) {
        return this.list.set(index, element);
    }

    @Override
    public void add(int index, E element) {
        this.list.add(index, element);
    }

    @Override
    public E remove(int index) {
        return this.list.remove(index);
    }

    @Override
    public int indexOf(Object o) {
        return this.list.indexOf(o);
    }

    @Override
    public int lastIndexOf(Object o) {
        return this.list.lastIndexOf(o);
    }

    @Override
    public ListIterator<E> listIterator() {
        return this.list.listIterator();
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return this.list.listIterator(index);
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return this.list.subList(fromIndex, toIndex);
    }

    @Override
    public Spliterator<E> spliterator() {
        return this.list.spliterator();
    }

    @Override
    public boolean removeIf(Predicate<? super E> filter) {
        return this.list.removeIf(filter);
    }

    @Override
    public Stream<E> stream() {
        return this.list.stream();
    }

    @Override
    public Stream<E> parallelStream() {
        return this.list.parallelStream();
    }

    @Override
    public void forEach(Consumer<? super E> action) {
        this.list.forEach(action);
    }
}
